package p.sunmes.les.action;

import com.badlogic.gdx.scenes.scene2d.Action;

public class GravityFallAction extends Action {
	public static final float G = 9.8f;

	private float vx, vy;
	private float sx, sy;
	private float ct;

	private float limitY = 0;

	public GravityFallAction(float vX, float vY) {
		this(vX, vY, 0);
	}

	public GravityFallAction(float vX, float vY, float limitY) {
		this.vx = vX;
		this.vy = vY;
		this.limitY = limitY;
	}

	public void setLimitY(float y) {
		limitY = y;
	}

	/**
	 * v=g*t
	 * h=1/2v*t
	 */
	@Override
	public boolean act(float delta) {
		if (ct == 0) {
			sx = target.getX();
			sy = target.getY();
			if (limitY == 0) {
				double ab2 = target.getWidth() * target.getWidth()
						+ target.getHeight() * target.getHeight();
				if (ab2 > 0) {
					limitY = -(float) Math.sqrt(ab2);
				}
			}
		}
		ct += delta * 15;
		float y = vy * ct - 0.5f * G * ct * ct;
		float x = vx * ct;

		target.setPosition(sx + x, sy + y);
		//		target.moveBy(x, y);

		if (target.getY() < limitY) {
			return true;
		}

		return false;
	}

}
